package com.starblink.product.view

import android.content.DialogInterface
import android.util.Log
import android.view.View
import androidx.fragment.app.FragmentActivity
import com.starblink.basic.apollo.ext.ApolloValSetExt
import com.starblink.android.basic.ext.toBean
import com.starblink.android.basic.ext.toJson
import com.starblink.android.basic.util.bus.FlowBus
import com.starblink.android.basic.util.bus.FlowConst
import com.starblink.android.basic.util.bus.bean.CollectProductEvent
import com.starblink.android.basic.util.bus.bean.FollowStoreEvent
import com.starblink.android.basic.widget.dialog.BaseBottomSheetFragment
import com.starblink.basic.ext.appScreenHeight
import com.starblink.product.databinding.DialogMoutedProductBinding
import com.starblink.product.detail.dialog.StoreReviewVM
import com.starblink.rocketreserver.fragment.ProductD
import com.starblink.rocketreserver.fragment.ProductF

/**
 * 挂载商品弹窗
 */
class ProductMountedDialog(
    val context: FragmentActivity,
    val productF: ProductF,
    val pageTrackTag: String,
    val dismissCall: () -> Unit
) : BaseBottomSheetFragment() {
    lateinit var mBinding: DialogMoutedProductBinding
    lateinit var viewModel: StoreReviewVM

    override fun getBindingRootView(): View {
        mBinding = DialogMoutedProductBinding.inflate(layoutInflater)
        return mBinding.root
    }

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        dismissCall.invoke()

    }

    override fun initView() {
        setPeekHeight(appScreenHeight - 44)
        mBinding.card.dialog = dialog
        var product = productF.toJson().toBean<ProductD>()
        mBinding.card.bindPost(context, product, 0, "", true, pageTrackTag = pageTrackTag) {
//            dismiss()
        }

        mBinding.close.setOnClickListener {
            dismiss()
        }


        //同步订阅数据
        FlowBus.with<FollowStoreEvent>(FlowConst.FOLLOW_STORE_EVENT).register(this) { event ->
            if (event.id != null && event.id == product.merchantWeb?.id
                && event.followed != product.merchantWeb!!.subOrNot
            ) {
                ApolloValSetExt.setApolloVal<ProductD.MerchantWeb>(
                    product.merchantWeb!!,
                    "subOrNot",
                    event.followed
                )
                Log.e("ProductMountedDialog", "===${product.merchantWeb}")
                mBinding.card.bindPost(context, product, 0, "", true, pageTrackTag = pageTrackTag)
            }

        }

        //同步收藏数据
        FlowBus.with<CollectProductEvent>(FlowConst.COLLECT_PRODUCT_EVENT).register(this) { event ->
            if (event.id != null && event.id == product.id && event.collect != product.collected
            ) {
                ApolloValSetExt.setApolloVal<ProductD>(
                    product,
                    "collected",
                    event.collect
                )
                mBinding.card.bindPost(context, product, 0, "", true, pageTrackTag = pageTrackTag)
            }
        }
    }

}